What is @faker-js/faker?
The @faker-js/faker package is a powerful tool for generating massive amounts of fake (but realistic) data for various purposes such as testing, bootstrapping databases, and creating rich prototypes. It can create data for names, addresses, phone numbers, and much more.
What are @faker-js/faker's main functionalities?
Basic random data generation
Generate random names, addresses, and other basic personal information.
const { faker } = require('@faker-js/faker');
console.log(faker.name.findName()); // Generates a random name
Localization
Support for localized data, allowing generation of data that is locale-specific.
const { faker } = require('@faker-js/faker');
faker.locale = 'de';
console.log(faker.name.findName()); // Generates a random name with German locale
Random numbers
Create random numbers, including options for specifying ranges and precision.
const { faker } = require('@faker-js/faker');
console.log(faker.datatype.number()); // Generates a random number
Date and time
Generate random dates and times, with the ability to specify past or future dates.
const { faker } = require('@faker-js/faker');
console.log(faker.date.future()); // Generates a random future date
Internet-related data
Create fake internet data such as email addresses, domain names, and URLs.
const { faker } = require('@faker-js/faker');
console.log(faker.internet.email()); // Generates a random email address
Commerce data
Generate commerce-related data like product names, prices, and categories.
const { faker } = require('@faker-js/faker');
console.log(faker.commerce.productName()); // Generates a random product name
Images
Create URLs for random placeholder images.
const { faker } = require('@faker-js/faker');
console.log(faker.image.imageUrl()); // Generates a random image URL
Other packages similar to @faker-js/faker
chance
Chance is a minimalist generator of random strings, numbers, etc. to help reduce some monotony particularly while writing automated tests or anywhere else you need anything random.
casual
Casual is a fake data generator for JavaScript which is more lightweight than @faker-js/faker and provides an easy-to-use API with a smaller set of functionalities.
randexp
Randexp generates strings that match a given regular expression which is useful for creating random data that fits a specific pattern, unlike @faker-js/faker which provides a wide range of predefined data types.
⚡️ Try it Online
API Documentation
🚀 Features
- 💌 Addresses - Generate valid looking Addresses, Zip Codes, Street Names, States, and Countries!
- ⏰ Time-based Data - Past, present, future, recent, soon... whenever!
- 🌏 Localization - Set a locale to generate realistic looking Names, Addresses, and Phone Numbers.
- 💸 Finance - Create stubbed out Account Details, Transactions, and Crypto Addresses.
- 👠 Products - Generate Prices, Product Names, Adjectives, and Descriptions.
- 👾 Hacker Jargon - “Try to reboot the SQL bus, maybe it will bypass the virtual application!”
- 🧍 Names - Generate virtual humans with a complete online and offline identity.
- 🔢 Numbers - Of course, we can also generate random numbers and strings.
Note: Faker tries to generate realistic data and not obvious fake data.
The generated names, addresses, emails, phone numbers, and/or other data might be coincidentally valid information.
Please do not send any of your messages/calls to them from your test setup.
📦 Install
npm install --save-dev @faker-js/faker
🪄 Usage
import { faker } from '@faker-js/faker';
export const USERS: User[] = [];
export function createRandomUser(): User {
return {
userId: faker.datatype.uuid(),
username: faker.internet.userName(),
email: faker.internet.email(),
avatar: faker.image.avatar(),
password: faker.internet.password(),
birthdate: faker.date.birthdate(),
registeredAt: faker.date.past(),
};
}
Array.from({ length: 10 }).forEach(() => {
USERS.push(createRandomUser());
});
💎 Modules
An in-depth overview of the API methods is available in the documentation.
The API covers the following modules:
Module | Example | Output |
---|
Address | faker.address.city() | Lake Raoulfort |
Animal | faker.animal.cat() | Norwegian Forest Cat |
Color | faker.color.rgb() | #cdfcdc |
Commerce | faker.commerce.product() | Polo t-shirt |
Company | faker.company.companyName() | Zboncak and Sons |
Database | faker.database.engine() | MyISAM |
Datatype | faker.datatype.uuid() | 7b16dd12-935e-4acc-8381-b1e457bf0176 |
Date | faker.date.past() | Sat Oct 20 2018 04:19:38 GMT-0700 (Pacific Daylight Time) |
Finance | faker.finance.amount() | ¥23400 (After setting locale) |
Git | faker.git.commitMessage() | feat: add products list page |
Hacker | faker.hacker.phrase() | Try to reboot the SQL bus, maybe it will bypass the virtual application! |
Helpers | faker.helpers.arrayElement(['a', 'b', 'c']) | b |
Image | faker.image.cats() | https://loremflickr.com/640/480/cats |
Internet | faker.internet.domainName() | muddy-neuropathologist.net |
Lorem | faker.lorem.paragraph() | Porro nulla id vero perspiciatis nulla nihil. ... |
Music | faker.music.genre() | R&B |
Name | faker.name.firstName() | Cameron |
Phone | faker.phone.phoneNumber() | +1 291-299-0192 |
Random | faker.random.locale() | fr_CA |
Science | faker.science.unit() | { name: 'meter', symbol: 'm' } |
System | faker.system.directoryPath() | /root |
Vehicle | faker.vehicle.vehicle() | Lamborghini Camry |
Word | faker.word.adjective() | adorable |
Templates
Faker contains a generator method faker.helpers.fake
for combining faker API methods using a mustache string format.
console.log(
faker.helpers.fake(
'Hello {{name.prefix}} {{name.lastName}}, how are you today?'
)
);
🌏 Localization
Faker has support for multiple locales.
The default language locale is set to English.
Setting a new locale is simple:
faker.locale = 'de';
See our documentation for a list of provided languages
Please note: not every locale provides data for every module. In our pre-made locales, we fallback to English in such a case as this is the most complete and most commonly used language.
⚙️ Setting a randomness seed
If you want consistent results, you can set your own seed:
faker.seed(123);
const firstRandom = faker.datatype.number();
faker.seed(123);
const secondRandom = faker.datatype.number();
console.log(firstRandom === secondRandom);
Faker is an MIT-licensed open source project with its ongoing development made possible entirely by the support of these awesome backers
Backers
✨ Contributing
Please make sure to read the Contributing Guide before making a pull request.
📘 Credits
Thanks to all the people who already contributed to Faker!
The fakerjs.dev website is kindly hosted by the Netlify Team. Also the search functionality is powered by algolia.
📝 Changelog
Detailed changes for each release are documented in the release notes.
📜 What happened to the original faker.js?
Read the team update (January 14th, 2022).
🔑 License
MIT